package com.example.springsecurity.controller;


import com.example.springsecurity.common.annotation.SystemLogInterface;
import com.example.springsecurity.common.enums.BusinessTypeEnum;
import com.example.springsecurity.common.req.SystemMenuReq;
import com.example.springsecurity.common.resp.ResponseResult;
import com.example.springsecurity.entity.SystemMenu;
import com.example.springsecurity.service.SystemMenuService;
import com.example.springsecurity.common.vo.SystemMenuListVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

import static com.example.springsecurity.common.resp.ResponseResult.success;

/**
 * 系统菜单控制层
 *
 * @author aman
 * @since 2025-01-15 16:59:00
 */
@RestController
@Tag(name = "系统菜单管理")
@RequestMapping("/system/menu")
public class SystemMenuController {

    @Resource
    private SystemMenuService systemMenuService;

    /**
     * @Description：查询菜单路由名称列表
     * @Author：fengcheng
     * @Date：2025/1/17 09:47
     * @ParamsType：[]
     * @Return：com.example.springsecurity.common.response.ResponseResult<java.util.List<java.lang.String>>
     */

    @Operation(summary = "查询菜单路由名称列表")
    @PostMapping("/queryRouteNameList")
    @PreAuthorize("hasAnyAuthority('perm:manage_menu')")
    @SystemLogInterface(title = "查询菜单路由名称列表", businessType = BusinessTypeEnum.QUERY)
    public ResponseResult<List<String>> queryRouteNameList() {
        List<SystemMenu> systemMenuList = systemMenuService.list();
        return success(systemMenuList.stream().map(SystemMenu::getRouteName).toList());
    }

    /*
    @PostMapping("/queryMenuList")
    @SystemLogInterface(title = "分页查询菜单列表", businessType = BusinessTypeEnum.QUERY)
    public ResponseResult<IPage<SystemMenu>> queryMenuList(Page<SystemMenu> page, SystemMenu systemMenu) {
        return success(this.systemMenuService.page(page, new QueryWrapper<>(systemMenu)));
    }
    */

    /**
     * @Description：查询菜单列表
     * @Author：fengcheng
     * @Date：2025/1/17 09:47
     * @ParamsType：[]
     * @Return：com.example.springsecurity.common.response.ResponseResult<java.util.List<java.lang.String>>
     */
    @Operation(summary = "查询菜单列表")
    @PostMapping("/queryMenuList")
    @SystemLogInterface(title = "查询菜单列表", businessType = BusinessTypeEnum.QUERY)
    public ResponseResult<List<SystemMenuListVo>> queryMenuList() {
        return success(systemMenuService.queryMenuList());
    }

    /**
     * @Description：添加菜单信息
     * @Author：fengcheng
     * @Date：2025/1/17 09:48
     * @Param：systemMenu
     * @ParamsType：[com.example.springsecurity.entity.SystemMenu]
     * @Return：com.example.springsecurity.common.response.ResponseResult<java.lang.Boolean>
     */
    @Operation(summary = "添加菜单信息")
    @PostMapping("/insertMenu")
    @SystemLogInterface(title = "添加菜单信息", businessType = BusinessTypeEnum.INSERT)
    public ResponseResult<Boolean> insertMenu(@RequestBody SystemMenuReq systemMenuReq) {
        return success(systemMenuService.insertMenu(systemMenuReq));
    }

    /**
     * @Description：更新菜单信息
     * @Author：fengcheng
     * @Date：2025/1/17 09:49
     * @Param：systemMenu
     * @ParamsType：[com.example.springsecurity.entity.SystemMenu]
     * @Return：com.example.springsecurity.common.response.ResponseResult<java.lang.Boolean>
     */
    @Operation(summary = "更新菜单信息")
    @PostMapping("/updateMenu")
    @SystemLogInterface(title = "更新菜单信息", businessType = BusinessTypeEnum.UPDATE)
    public ResponseResult<Boolean> updateMenu(@RequestBody SystemMenuReq systemMenuReq) {
        return success(systemMenuService.updateMenu(systemMenuReq));
    }

    /**
     * @Description：删除菜单信息
     * @Author：fengcheng
     * @Date：2025/1/17 09:49
     * @Param：idList
     * @ParamsType：[java.util.List<java.lang.Long>]
     * @Return：com.example.springsecurity.common.response.ResponseResult<java.lang.Boolean>
     */
    @Operation(summary = "删除菜单信息")
    @PostMapping("/deleteMenu")
    @SystemLogInterface(title = "删除菜单信息", businessType = BusinessTypeEnum.DELETE)
    public ResponseResult<Boolean> deleteMenu(@RequestBody List<String> idList) {
        // removeByIds, 第二个参数为：true，表示物理删除，false，表示逻辑删除
        return success(this.systemMenuService.removeByIds(idList, false));
    }
}
